<template>
	<!-- <view>
			<web-view  :src="url"   @message="getMessage"></web-view>
		</view> -->

	<view :style="{height: pageHeight+'px',marginTop:pageTop+'px' }" class="gui-bg-gray">
		<!-- <view class="header"  style="align-content: center;" >
				<view style="padding-left: 20rpx;width:20%" @click="goBack()">
					<text class="gui-icons">&#xe600;</text>	
				</view>
				<view>
					{{title}}
				</view>
				<view class="gui-flex jc-center" @click="goBack(0)" style="width:20%">
					<text class="gui-icons">&#xe78a;</text>	
				</view>	
			</view> -->

		<view>
			<web-view ref="webview" class="border" :fullscreen="true" :src="url" 
			@message="getMessage"></web-view>
		</view>
	</view>

</template>

<script>
	var sweixin;
	import grace from '../../Grace6/js/grace';
	// import encBase64url from 'crypto-js/enc-base64url';
	var wv;
	export default {
		data() {
			return {
				Bearer: '',
				pageTop: 0,
				title: '',
				url: '',
				nav_height: 96,
				webviewStyles: {
					progress: {
						color: '#FF3333',
					}
				},
				homePage: '/pages/switchPages/main',
				pageHeight: '300rpx',
				back_page: '',
				// title: null,
				// url: "https://app.lgwisdomunion/app.html#/craftsmanSchool/index",
				// localUrl: localUrl,
				// index: 0,
				// msg: "",
				// width: 400,
				// height: 400,
				// config: {
				// 	progressColor: "#ff00ff",
				// 	scrollIndicator: false
				// },
				// testUrl: "http://192.168.11.10:8080/cookie/a",
				// testHost: "192.168.11.10",
				member: null,
				user: null,
				token: null,

			}
		},
		onLoad(opt) {
			this.member = uni.getStorageSync("member");
			this.user = uni.getStorageSync("userInfo");
			this.token = uni.getStorageSync("ttsxAuthToken");
			this.Bearer = uni.getStorageSync("Bearer");
			console.log("web->opt", opt);
			let info = uni.getWindowInfo();
			if (opt.back) {
				this.back_page = opt.back;
				console.log("back_page", this.back_page);
			}
			if (opt.title) {
				uni.setNavigationBarTitle({
					title: opt.title
				})
				this.title = opt.title;
			}
			let sysInfo = uni.getSystemInfoSync();
			console.log("sysInfo", sysInfo);
			this.pageTop = sysInfo.safeArea.top;
			console.log("pageTop", this.pageTop);
			this.pageHeight = info.windowHeight - this.pageTop;
			//this.nav_height=info.pixelRatio*info.safeArea.top;
			console.log("nav_height", this.nav_height);
			let userInfo = this.camelCaseJsonKeys(uni.getStorageSync("userInfo"));
			if (!userInfo) uni.redirectTo({
				url: "/pages/login/login"
			});
			if(opt.hasOwnProperty("Bearer")){
				
				this.url=opt.url+"?Bearer="+this.Bearer;
			}else{
				this.$boya.NGet("/tpi/mcode", null, function(res) {
					console.log("mcode", res);
					self.code = res.data;
					opt.url = opt.url + "?code=" + res.data;
					this.url_to(opt);
				})
			}

			// this.weixin();
			

		},
		// onReady() {
		// 	console.log("onReady");
		// 		var currentWebview = this.$scope.$getAppWebview(); //获取当前页面的webview对象,此对象相当于html5plus里的plus.webview.currentWebview()        
		// 		setTimeout(function() {
		// 			//向web写入Bearer token

		// 			let webview = currentWebview.children()[0];  
		// 			let Bearer=uni.getStorageSync("Bearer");
		// 			console.log("Bearer",Bearer);
		// 			this.Bearer=Bearer;

		// 			  //let js=`uni.setStorageSync("Bearer","${Bearer}"); uni.showToast({	title: "${Bearer}",duration: 2000})`;
		// 			  let js=`uni.setStorageSync("Bearer","${Bearer}");`;
		// 			//webview.evalJS("window.localStorage.setItem('Bearer',''"+uni.getStorageSync("Bearer")+"');console.log('bearer')");  
		// 			webview.evalJS(js);  

		// 		}, 2000); //如果是页面初始化调用时，需要延时一下   
		// },
		// onReady() {
		// 	// #ifdef APP-PLUS
		// 	var currentWebview = this.$scope
		// 		.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效

		// 	setTimeout(function() {
		// 		wv = currentWebview.children()[0]

		// 		let info = uni.getSystemInfoSync();
		// 		console.log("systemInfo", info);
		// 		//wv.setStyle({top:86,height:600})		

		// 		//wv.setStyle({top: info.statusBarHeight,height:info.screenHeight-info.statusBarHeight})
		// 		wv.setStyle({
		// 			top: 96,
		// 			height: info.screenHeight - (info.statusBarHeight * 2) - 20
		// 		})

		// 	}, 1000); //如果是页面初始化调用时，需要延时一下
		// 	// #endif
		// },
		onReady() {
			// #ifdef APP-PLUS
			let self = this;
			var currentWebview = this.$scope
				.$getAppWebview()
			setTimeout(function() {
				wv = currentWebview.children()[0]
				let info = uni.getSystemInfoSync();
				console.log("systemInfo", info);
				// wv.setStyle({
				// 	top: 96 ,
				// 	height: info.safeArea.height- 96
				// })
				//self.headerStyle.hegiht=info.statusBarHeight+"px";

				let Bearer = uni.getStorageSync("Bearer");

				webview.evalJS(`uni.$emit('Bearer',{Bearer:'${Bearer}'})`);


			}, 500);
			// #endif
		},

		onNavigationBarButtonTap(e) {
			console.log("onNavigationBarButtonTap", e);
			uni.redirectTo({
				url: "/pages/switchPages/main"
			})
		},
		methods: {

			weixin() {
				
			},

			url_to(parm) {

				this.url=this.$comm.web_url(parm);

				console.log("web->url", this.url);
			},
			StrToObj(queryString) {
				const params = {};
				const queryStringWithoutQuestionMark = queryString.substring(1); // 去掉问号
				const keyValuePairs = queryStringWithoutQuestionMark.split('&');

				for (let pair of keyValuePairs) {
					const [key, value] = pair.split('=');
					params[key] = value;
				}

				return params;
			},

			objToStr(obj) {
				let paramsText = "";
				for (const i in obj) {
					if (obj[i]) {
						paramsText += i + "=" + obj[i] + "&";
					}
				}
				paramsText = paramsText.slice(0, paramsText.length - 1);
				return paramsText;
			},
			camelCaseJsonKeys(json) {
				const result = {};

				for (const key in json) {


					const camelKey = key.replace(/_(\w)/g, function(match, p1) {
						return p1.toUpperCase();
					});
					result[camelKey] = json[key];
				}
				console.log('userInfo: ', result);
				return result;
			},
			aa() {
				uni.setClipboardData({
					data: this.url,
					success() {
						uni.showToast({
							title: "ok"
						})
					}
					// "/craftsmanSchool/index"
					//  /craftsmanSchool/index
					//http://lg.boyaltd.cn/#/login?login=eyJ1c2VySW5mbyI6eyJ1c2VyTmFtZSI6IjE5ODIxMDI0IiwicmVhbE5hbWUiOiLpmYjlpKfotoUiLCJoZWFkUG9ydHJhaXQiOiIiLCJzdGF0dXMiOjAsInBob25lTnV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6ImM2OWE3MGM1LWRlNmYtNDRmYS1hMzMzLTg4YzVhNWIzZmY3NyIsInRva2VuIjoiZWU5YzU5NWUtYWZiMy00YWI4LWFiYTMtMGY1ZGE0NzY1MTYyIiwidXJsIjoiL2NyYWZ0c21hblNjaG9vbC9pbmRleCJ9
				})

			},
			goBack(n) {
				if (n == 0) {
					uni.redirectTo({
						url: this.homePage
					})
				} else {

					uni.navigateBack({
						delta: 1
					});
				}

			},
			goHome() {
				uni.reLaunch({
					url: "/" + this.back_page + "?back=web"
				})
			},
			getMessage(e) {
				let self=this;
				let dev=uni.getStorageSync("is_PreviewVer")
				if(dev){
					uni.showModal({
						title:"接收参数",
						content: JSON.stringify(e.detail),
						showCancel: false,
						success:function(){
							self.$comm.web_message(e);
						}
					})
				}else{
					this.$comm.web_message(e);
				}	
			}
		}
	}
</script>

<style>
	/* #ifndef APP-NVUE */
	/* page{background-color: #3688FF;} */
	/* #endif */
	/* .header_btn{
		position: fixed;
		z-index: 99999;
		right: 20rpx;
		top: 20rpx;
		
	} */
	
	page{
		width: 100%;
		position: fixed;
		top: 0rpx;
		bottom: 0rpx;
		height: 100%;
		background-color: #ccc;
	}
	
	.header {
		background-color: #f95053;
		color: #ffffff;
		position: fixed;
		z-index: 999999;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;


		font-size: 36rpx;
	}
</style>